package com.hiss.basic.admin.listener;

import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.parameter.SaLoginParameter;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.hiss.basic.model.system.domain.SysUserDomain;
import com.hiss.basic.model.system.pojo.AuthenticationOnlineModel;
import com.hiss.basic.starter.core.utils.ServletUtils;
import com.hiss.basic.starter.log.event.LoginLogEvent;
import com.hiss.basic.starter.security.helper.SecurityHelper;
import com.hiss.basic.system.service.cache.AuthenticationOnlineCache;
import com.mybatisflex.core.update.UpdateChain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 认证行为监听器
 */
@Slf4j
@Component
public class AuthenticationActionListener implements SaTokenListener {

    @Resource
    private AuthenticationOnlineCache authenticationOnlineCache;

    @Override
    public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginParameter loginModel) {
        Long userId = (Long) loginModel.getExtra(SecurityHelper.USER_ID_FIELD);
        String username = (String) loginModel.getExtra(SecurityHelper.USERNAME_FIELD);
        String nickname = (String) loginModel.getExtra(SecurityHelper.NICKNAME_FIELD);

        HttpServletRequest request = ServletUtils.getRequest();

        LocalDateTime loginTime = LocalDateTime.now();

        // 记录在线用户
        AuthenticationOnlineModel onlineModel = new AuthenticationOnlineModel();
        onlineModel.setToken(tokenValue);
        onlineModel.setUserId(userId);
        onlineModel.setUsername(username);
        onlineModel.setNickname(nickname);
        onlineModel.setIp(ServletUtil.getClientIP(request));
        UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
        onlineModel.setOs(userAgent.getOs().getName());
        onlineModel.setBrowser(userAgent.getBrowser().getName());
        onlineModel.setLoginTime(loginTime);
        onlineModel.setExpireTime(loginTime.plusSeconds(loginModel.getTimeout()));
        onlineModel.setTimeout(loginModel.getTimeout());
        authenticationOnlineCache.put(onlineModel);

        // 记录登录日志
        LoginLogEvent event = new LoginLogEvent();
        event.setUsername(username);
        event.setStatus(0);
        event.setRequest(ServletUtils.getRequest());
        SpringUtil.getApplicationContext().publishEvent(event);

        // 记录登录时间
        UpdateChain.of(SysUserDomain.class)
                .set(SysUserDomain::getLastLoginTime, loginTime)
                .where(SysUserDomain::getId).eq(userId)
                .update();
    }

    @Override
    public void doLogout(String loginType, Object loginId, String tokenValue) {
        authenticationOnlineCache.remove(tokenValue);
    }

    @Override
    public void doKickout(String loginType, Object loginId, String tokenValue) {
        authenticationOnlineCache.remove(tokenValue);
    }

    @Override
    public void doReplaced(String loginType, Object loginId, String tokenValue) {
        authenticationOnlineCache.remove(tokenValue);
    }

    @Override
    public void doDisable(String loginType, Object loginId, String service, int level, long disableTime) {

    }

    @Override
    public void doUntieDisable(String loginType, Object loginId, String service) {

    }

    @Override
    public void doOpenSafe(String loginType, String tokenValue, String service, long safeTime) {

    }

    @Override
    public void doCloseSafe(String loginType, String tokenValue, String service) {

    }

    @Override
    public void doCreateSession(String id) {

    }

    @Override
    public void doLogoutSession(String id) {

    }

    @Override
    public void doRenewTimeout(String tokenValue, Object loginId, long timeout) {

    }
}
